package com.dragons.spring5.jdbcTemplate.dao;

import com.dragons.spring5.jdbcTemplate.entities.User5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;//注入 JdbcTemplate

    @Override
    public void add(User5 user5) {
        //第一个参数：sql 语句 第二个参数：可变参数，设置 sql 语句值
        String sql = "insert into t_user values(?,?,?)";
        Object[] args = {user5.getUserId(),user5.getUsername(),user5.getUstatus()};
        jdbcTemplate.update(sql,args);
    }


    @Override//修改
    public void updateBook(User5 user5) {
        String sql = "update t_user set username=?,ustatus=? where user_id=?";
        Object[] args = {user5.getUsername(), user5.getUstatus(),user5.getUserId()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override//删除
    public void delete(String id) {
        String sql = "delete from t_user where user_id=?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println(update);
    }

    @Override
    public int selectCount() {
        String sql = "select count(*) from t_user";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    //查询返回对象
    @Override
    public User5 findBookInfo(String id) {
        String sql = "select * from t_user where user_id=?";
        //调用方法
        User5 user5 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User5>(User5.class), id);
        return user5;
    }

    //查询返回集合
    @Override
    public List<User5> findAllBook() {
        String sql = "select * from t_user";
        //调用方法
        List<User5> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User5>(User5.class));
        return bookList;
    }

    //批量添加
    @Override
    public void batchAddBook(List<Object[]> batchArgs) {
        String sql = "insert into t_user values(?,?,?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }
    //批量添加测试
    //List<Object[]> batchArgs = new ArrayList<>();
    //Object[] o1 = {"3","java","a"};
    //Object[] o2 = {"4","c++","b"};
    //Object[] o3 = {"5","MySQL","c"};
    //batchArgs.add(o1);
    //batchArgs.add(o2);
    //batchArgs.add(o3);
    ////调用批量添加
    //bookService.batchAdd(batchArgs);




    //批量修改
    @Override
    public void batchUpdateBook(List<Object[]> batchArgs) {
        String sql = "update t_user set username=?,ustatus=? where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }
    //批量修改
    //List<Object[]> batchArgs = new ArrayList<>();
    //Object[] o1 = {"java0909","a3","3"};
    //Object[] o2 = {"c++1010","b4","4"};
    //Object[] o3 = {"MySQL1111","c5","5"};
    //batchArgs.add(o1);
    //batchArgs.add(o2);
    //batchArgs.add(o3);
    ////调用方法实现批量修改
    //bookService.batchUpdate(batchArgs);



    //批量删除
    @Override
    public void batchDeleteBook(List<Object[]> batchArgs) {
        String sql = "delete from t_user where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }
    //批量删除
    //List<Object[]> batchArgs = new ArrayList<>();
    //Object[] o1 = {"3"};
    //Object[] o2 = {"4"};
    //batchArgs.add(o1);
    //batchArgs.add(o2);
    ////调用方法实现批量删除
    //bookService.batchDelete(batchArgs);
}

